package com.growthsee.rider.task;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.growthsee.common.utils.StringUtils;
import com.growthsee.rider.domain.RiderInfo;
import com.growthsee.rider.enumeration.RegisterEnum;
import com.growthsee.rider.service.RiderInfoService;
import com.growthsee.rider.utils.ApplicationContexts;
import com.growthsee.rider.wrapper.RiderWrapper;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author growthsee
 */
@Component("riderTask")
public class RiderTask {
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }

    /**
     *  每季度初执行初始化计分和消分
     * @param userId
     */
    public void initRiderInfo(String userId) {
        RiderInfoService riderInfoService = ApplicationContexts.getBean(RiderInfoService.class);
        LambdaUpdateWrapper<RiderInfo> lambdaUpdateWrapper = Wrappers.<RiderInfo>lambdaUpdate()
                .set(RiderInfo::getCumulativeScore,new BigDecimal(0))
                .set(RiderInfo::getCurrentQuarterRemoveScore,new BigDecimal(0));
        if(StringUtils.isNotEmpty(userId)){
            lambdaUpdateWrapper.eq(RiderInfo::getUserId,userId);
        }
        riderInfoService.update(lambdaUpdateWrapper);
        System.out.println("执行无参方法");
    }

    //再次推送失败数据
    public void refresh(){
        RiderInfoService riderInfoService = ApplicationContexts.getBean(RiderInfoService.class);
        List<RiderInfo> list = riderInfoService.list(Wrappers.<RiderInfo>lambdaQuery()
                .eq(RiderInfo::getRegisterStatus, RegisterEnum.REGISTERED.getValue())
                .ne(RiderInfo::getPlatformCompanyId,null)
                .apply("not exists ( select log.rider_id from rider_push_log log where (log.status = '200' || log.status = '400') and log.rider_id = id )"));
        if(CollectionUtils.isNotEmpty(list)){
            for (RiderInfo riderInfo : list) {
                RiderWrapper.builder().pushData(riderInfo);
            }
        }
    }


    /**
     * 同步扣分行为
     */
    public void synchronousDeductAction() {

        //1.获取扣分数据列表
        //2.插入扣分行为
        //3.更新对应骑手的当前计分
        //4.判断上月是否没有有扣分记录，满足条件->5
        //5.判断满足季度最后一天,条件满足->6,不满足->7
        //6.扣除当前计分6分6分内直接消除
        //7.判断计分是否>=6分，满足条件->8
        //8.当前计分直接扣除6分，天机消分记录
        //9。修改骑手状态

        System.out.println("执行无参方法");
    }





}
